package com.ss.whisper2.controller;

import com.ss.whisper2.pojo.entity.user.RequestUser;
import com.ss.whisper2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@ControllerAdvice
@Controller
public class MemberController {
	@Autowired
	UserService userService;
	@RequestMapping("/member-list")
	public String getMemberList(HttpSession session) {
		session.setAttribute("memberList",userService.getUserList());
		return "member-list";
	}
	@PostMapping("/add")
	@ResponseBody
	public String addMember(@RequestBody RequestUser reqUser) {
		//System.out.println(reqUser);
		userService.addUser(reqUser);
		return "成功";
	}
	@PostMapping("/delete")
	@ResponseBody
	public String deleteMember(@RequestBody String username) {
		//System.out.println(username);
		userService.deleteUser(username);
		return "成功";
	}
	@PostMapping("/bulkDelete")
	@ResponseBody
	public String bulkDeleteMember(@RequestBody String usernames) {
		String[] username = usernames.split(",");
		for (String s : username) {
			//userService.deleteUser(s);
		}
		return "成功";
	}
	@PostMapping("/edit")
	@ResponseBody
	public String editMember(@RequestBody RequestUser reqUser) {
		userService.setUser(reqUser);
		return "成功";
	}

	@RequestMapping("/member-password")
	public String toSetMemberPassword() {
		return "member-password";
	}

	@PostMapping("/password")
	@ResponseBody
	public String setMemberPassword(@RequestBody RequestUser reqUser) {
		userService.setUser(reqUser);
		return "成功";
	}
}
